//package myException.myRestfulGolbalException;
//
//import jakarta.servlet.http.HttpServletRequest;
//import org.springframework.dao.DataIntegrityViolationException;
//import org.springframework.dao.DuplicateKeyException;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.converter.HttpMessageNotReadableException;
//import org.springframework.validation.BindException;
//import org.springframework.validation.FieldError;
//import org.springframework.web.HttpRequestMethodNotSupportedException;
//import org.springframework.web.bind.MethodArgumentNotValidException;
//import org.springframework.web.bind.MissingServletRequestParameterException;
//import org.springframework.web.bind.annotation.ExceptionHandler;
//import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.bind.annotation.ResponseStatus;
//import org.springframework.web.bind.annotation.RestControllerAdvice;
//import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
//
//import java.util.ArrayList;
//import java.util.Iterator;
//import java.util.List;
//import java.util.Set;
//
//@RestControllerAdvice
//public class GlobalExceptionHandler {
//    @ExceptionHandler(value = MethodArgumentTypeMismatchException.class)
//    @ResponseBody
//    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
//    public Result<Object> exceptionHandler(HttpServletRequest req, MethodArgumentTypeMismatchException e) {
//        // log.error("MethodArgumentTypeMismatchException", e);
//        return Result.error(ExceptionEnum.BODY_NOT_MATCH, e.getMessage());
//    }
//
//    //@ExceptionHandler(value = MissingServletRequestParameterException.class)
//    //@ResponseBody
//    //@ResponseStatus(value = HttpStatus.BAD_REQUEST)
//    //public Result<Object> exceptionHandler(HttpServletRequest req, MissingServletRequestParameterException e) {
//    //    // log.error("MissingServletRequestParameterException", e);
//    //    return Result.error(ExceptionEnum.BODY_NOT_MATCH, e.getMessage());
//    //}
//
//    @ExceptionHandler(value = HttpMessageNotReadableException.class)
//    @ResponseBody
//    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
//    public Result<Object> exceptionHandler(HttpServletRequest req, HttpMessageNotReadableException e) {
//        // log.error("HttpMessageNotReadableException", e);
//        return Result.error(ExceptionEnum.BODY_NOT_MATCH, e.getMessage());
//    }
//
//    //@ExceptionHandler(value = UnexpectedTypeException.class)
//    //@ResponseBody
//    //@ResponseStatus(value = HttpStatus.BAD_REQUEST)
//    //public Result<Object> exceptionHandler(HttpServletRequest req, UnexpectedTypeException e) {
//    //    // log.error("UnexpectedTypeException", e);
//    //    return Result.error(ExceptionEnum.BODY_NOT_MATCH, e.getMessage());
//    //}
//
//    @ExceptionHandler(value = DuplicateKeyException.class)
//    @ResponseBody
//    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
//    public Result<Object> exceptionHandler(HttpServletRequest req, DuplicateKeyException e) {
//        // log.error("DuplicateKeyException", e);
//        return Result.error(ExceptionEnum.INTERNAL_SERVER_ERROR, "duplicate key value violates unique constraint");
//    }
//
//    //@ExceptionHandler(value = BadSqlGrammarException.class)
//    //@ResponseBody
//    //@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
//    //public Result<Object> exceptionHandler(HttpServletRequest req, BadSqlGrammarException e) {
//    //    // log.error("BadSqlGrammarException", e);
//    //    return Result.error(ExceptionEnum.SQL_ERROR);
//    //}
//
//
//    @ExceptionHandler(value = NullPointerException.class)
//    @ResponseBody
//    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
//    public Result<Object> exceptionHandler(HttpServletRequest req, NullPointerException e) {
//        // log.error("发生空指针异常:", e);
//        return Result.error(ExceptionEnum.INTERNAL_SERVER_ERROR);
//    }
//
//    //@ExceptionHandler(value = NoResourceFoundException.class)
//    //@ResponseBody
//    //@ResponseStatus(value = HttpStatus.NOT_FOUND)
//    //public Result<Object> exceptionHandler(HttpServletRequest req, NoResourceFoundException e) {
//    //    return Result.error(ExceptionEnum.NOT_FOUND, ExceptionEnum.NOT_FOUND.getResultMsg());
//    //}
//
//
//    @ExceptionHandler(value = DataIntegrityViolationException.class)
//    @ResponseBody
//    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
//    public Result<Object> exceptionHandler(HttpServletRequest req, DataIntegrityViolationException e) {
//        // log.error("发生数据库异常:", e);
//        return Result.error(ExceptionEnum.SQL_ERROR, "请确认输入的参数是否正确" + e.getMessage());
//    }
//
//
//    //@ExceptionHandler(value = ConstraintViolationException.class)
//    //@ResponseBody
//    //@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
//    //public Result<Object> exceptionHandler(HttpServletRequest req, ConstraintViolationException e) {
//    //    // log.error("发生参数错误:", e);
//    //    Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
//    //    Iterator<ConstraintViolation<?>> iterator = constraintViolations.iterator();
//    //    List<String> msgList = new ArrayList<>();
//    //    while (iterator.hasNext()) {
//    //        ConstraintViolation<?> cvl = iterator.next();
//    //        msgList.add(cvl.getPropertyPath() + cvl.getMessage());
//    //    }
//    //    return Result.error(ExceptionEnum.SQL_ERROR, String.join(",", msgList));
//    //}
//
//    @ExceptionHandler(value = MethodArgumentNotValidException.class)
//    @ResponseBody
//    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
//    public Result<Object> exceptionHandler(HttpServletRequest req, MethodArgumentNotValidException e) {
//
//        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
//        List<String> msgList = new ArrayList<>();
//        for (FieldError fieldError : fieldErrors) {
//            msgList.add(fieldError.getField() + fieldError.getDefaultMessage());
//        }
//        return Result.error(ExceptionEnum.BODY_NOT_MATCH, String.join(",", msgList));
//    }
//
//    //@ExceptionHandler(value = MybatisPlusException.class)
//    //@ResponseBody
//    //@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
//    //public Result<Object> exceptionHandler(HttpServletRequest req, MybatisPlusException e) {
//    //    // log.error("发生数据库异常:", e);
//    //    return Result.error(ExceptionEnum.SQL_ERROR, "请确认需要更新的数据是否存在" + e.getMessage());
//    //}
//
//    @ExceptionHandler(value = Exception.class)
//    @ResponseBody
//    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
//    public Result<Object> exceptionHandler(HttpServletRequest req, Exception e) {
//        // log.error("未知异常:", e);
//        return Result.error(ExceptionEnum.INTERNAL_SERVER_ERROR);
//    }
//
//    @ExceptionHandler(value = BindException.class)
//    @ResponseBody
//    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
//    public Result<Object> exceptionHandler(HttpServletRequest req, BindException e) {
//        List<FieldError> fieldErrors = e.getFieldErrors();
//        List<String> msgList = new ArrayList<>();
//        for (FieldError fieldError : fieldErrors) {
//            msgList.add(fieldError.getField() + fieldError.getDefaultMessage());
//        }
//        return Result.error(ExceptionEnum.BODY_NOT_MATCH, String.join(",", msgList));
//    }
//
//    @ExceptionHandler(value = PermissionException.class)
//    @ResponseBody
//    @ResponseStatus(value = HttpStatus.UNAUTHORIZED)
//    public Result<Object> exceptionHandler(HttpServletRequest req, PermissionException e) {
//        return Result.error(ExceptionEnum.SIGNATURE_NOT_MATCH.getResultCode(), e.getMsg());
//    }
//
//    @ExceptionHandler(value = BizException.class)
//    @ResponseBody
//    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
//    public Result<Object> exceptionHandler(HttpServletRequest req, BizException e) {
//        return Result.error(ExceptionEnum.BUSINESS_SERVER_ERROR.getResultCode(), e.getMsg());
//    }
//
//    @ExceptionHandler(value = CommonException.class)
//    @ResponseBody
//    @ResponseStatus(value = HttpStatus.OK)
//    public Result<Object> exceptionHandler(HttpServletRequest req, CommonException e) {
//        return Result.error(ExceptionEnum.SUCCESS.getResultCode(), e.getMsg());
//    }
//
//    //@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
//    //@ResponseBody
//    //@ResponseStatus(value = HttpStatus.BAD_REQUEST)
//    //public Result<Object> exceptionHandler(HttpServletRequest req, HttpRequestMethodNotSupportedException e) {
//    //    return Result.error(ExceptionEnum.NOT_FOUND.getResultCode(), e.getMessage());
//    //}
//}
